<template>
  <div class="xtx_category">
    <!-- 顶级分类 -->
    <ul class="xtx_category_super">
      <li  @mouseenter="categoryId=1001">
        <a href="javascript:;">
          生鲜
          <small>水果</small>
          <small>蔬菜</small>
        </a>
        <i class="sprites"></i>
      </li>
      <li  @mouseenter="categoryId=1002">
        <a href="javascript:;">
          美食
          <small>面点</small>
          <small>干果</small>
        </a>
        <i class="sprites"></i>
      </li>
      <li  @mouseenter="categoryId=1003">
        <a href="javascript:;">
          电器
          <small>数码产品</small>
        </a>
        <i class="sprites"></i>
      </li>
      <li  @mouseenter="categoryId=1004">
        <a href="javascript:;">
          居家
          <small>床品</small>
          <small>四件套</small>
          <small>被枕</small>
        </a>
        <i class="sprites"></i>
      </li>
      <li  @mouseenter="categoryId=1005">
        <a href="javascript:;">
          洗护
          <small>洗发洗护</small>
          <small>美妆</small>
        </a>
        <i class="sprites"></i>
      </li>
      <li  @mouseenter="categoryId=1006">
        <a href="javascript:;">
          孕婴
          <small>奶粉</small>
          <small>玩具</small>
          <small>辅食</small>
        </a>
        <i class="sprites"></i>
      </li>
      <li  @mouseenter="categoryId=1007">
        <a href="javascript:;">
          餐橱
          <small>数码产品</small>
        </a>
        <i class="sprites"></i>
      </li>
      <li  @mouseenter="categoryId=1008">
        <a href="javascript:;">
          服饰
          <small>女装</small>
          <small>男装</small>
        </a>
        <i class="sprites"></i>
      </li>
      <li  @mouseenter="categoryId=1009">
        <a href="javascript:;">
          杂货
          <small>户外</small>
          <small>图书</small>
        </a>
        <i class="sprites"></i>
      </li>
      <li  @mouseenter="categoryId=1010">
        <a href="javascript:;">
          品牌
          <small>品牌制造</small>
        </a>
        <i class="sprites"></i>
      </li>
    </ul>
    <!-- 子分类 -->
    <div class="xtx_category_subset">
      <h4>分类推荐 <small>根据您的购买或浏览记录推荐</small></h4>
      <ul>
        <li v-for="item in goods" :key="item.id">
          <a :href="`#/product/${item.id}`">
            <img width="95" height="95" :src="item.picture" alt="">
            <div class="info">
              <p class="name">{{item.name}}</p>
              <p class="desc">{{item.desc}}</p>
              <p class="price"><i>￥</i>{{item.price}}</p>
            </div>
          </a>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  name: 'home-category',
  data () {
    return {
      categoryId: null,
      goods: [],
      timer: null
    }
  },
  watch: {
    categoryId () {
      if (this.timer) window.clearTimeout(this.timer)
      this.goods = []
      this.timer = window.setTimeout(() => {
        this.getGoods()
      }, 100)
    }
  },
  methods: {
    getGoods () {
      this.$http.get('/home/category?categoryId=' + this.categoryId).then(res => {
        this.goods = res.data.result
      })
    }
  }
}
</script>

<style scoped lang='less'>
// 分类
.xtx_category {
  width: 250px;
  background-color: rgba(0, 0, 0, 0.8);
  position: absolute;
  z-index: 9;
  &:hover .xtx_category_subset {
    display: block;
  }
}

.xtx_category_super {
  li {
    height: 50px;
    padding: 0 20px 0 40px;
    transition: background-color 0.25s;
    cursor: pointer;
  }

  li:hover,
  li.active {
    background-color: @xtxColor;
  }

  a {
    color: #fff;
    font-size: 16px;
    line-height: 50px;

    small {
      font-size: 14px;
      margin-right: 3px;
    }
  }

  i {
    display: block;
    width: 12px;
    height: 12px;
    margin-top: 20px;
    background-position: -75px -110px;
    float: right;
  }
}

.xtx_category_subset {
  width: 990px;
  height: 100%;
  background-color: rgba(255, 255, 255, 0.9);
  display: none;

  position: absolute;
  top: 0;
  left: 250px;
  padding: 0 15px;
  h4 {
    font-size: 20px;
    font-weight: normal;
    line-height: 80px;
    word-break: break-all;
    small {
      font-size: 16px;
      color: #666;
    }
  }
  ul {
    display: flex;
    flex-wrap: wrap;
    li {
      width: 310px;
      height: 120px;
      margin-right: 15px;
      margin-bottom: 15px;
      border: 1px solid #e4e4e4;
      border-radius: 4px;
      background: #fff;
      &:nth-child(3n) {
        margin-right: 0;
      }
      a {
        display: flex;
        width: 100%;
        height: 100%;
        align-items: center;
        padding: 0 10px;
        &:hover {
          background: #e3f9f4;
        }
        .info {
          padding-left: 10px;
          line-height: 24px;
          .name {
            font-size: 16px;
          }
          .desc {
            color: #666;
          }
          .price {
            font-size: 22px;
            color: @priceColor;
            i {
              font-size: 16px;
            }
          }
        }
      }
    }
  }
}
</style>
